<?php
import('@.Action.PublicAction');
class DatabackupAction extends Action
{
	//主页面
	function index(){
		$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
/* 		$sql="SHOW TABLES FROM `".C("DB_NAME")."`";//Tables_in_phptest
		$list=$Model->query($sql);
		//print_r($list);
 		$sql="SHOW TABLES FROM `".C("BACKUP_DB_NAME")."`";//Tables_in_phpbak
		$list=$Model->query($sql);
		print_r($list);		
		//print_r($list);
 		$sql="SHOW CREATE TABLE `phptest`.`xtu_studentinfo`";//Create Table
		$list=$Model->query($sql);
		print_r($list);*/
		//echo date("YmdHis");
		$sql="SHOW TABLES FROM `".C("BACKUP_DB_NAME")."`";//Tables_in_phpbak
		$list=$Model->query($sql);
		if($list)foreach($list as $tmp){
			$str=substr($tmp["Tables_in_".C("BACKUP_DB_NAME")],-14,14);
			$voList[$str]["backupdate"]=substr($str,0,4)."-".substr($str,4,2)."-".substr($str,6,2)." ".substr($str,8,2).":".substr($str,10,2).":".substr($str,12,2);
		}		
		$this->assign("list",$voList);
		$this->display();
	}
	//备份数据
	function backup(){
		$flag=date("YmdHis");
		$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
		//header('Content-Type: text/html; charset=UTF-8');
		//echo "开始备份<br>";
		//echo "当前时间:".$flag."<br>";
		//打印当前数据表
		$sql="SHOW TABLES FROM `".C("DB_NAME")."`";//Tables_in_phptest
		$list=$Model->query($sql);
		if($list)foreach($list as $tmp){
			//查询表结构
			$sql="SHOW CREATE TABLE `".C("DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."`";
			$create=$Model->query($sql);
			//建备份表
			$createSql=str_replace("`".$tmp["Tables_in_".C("DB_NAME")]."`","`".C("BACKUP_DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."_".$flag."`",$create[0]['Create Table']);
			$Model->execute($createSql);
			//备份数据到备份表
			$insertSql="INSERT INTO `".C("BACKUP_DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."_".$flag."` SELECT * FROM `".C("DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."` ;";
			$Model->execute($insertSql);
			//echo "备份完成:".$tmp["Tables_in_".C("DB_NAME")]."<br>";
		}
		/*echo "<script>alert('数据备份完成!');</script>";*/
		$this->assign("jumpUrl",__URL__); 				
		$this->success("数据备份成功!"); 
	}		
	//数据恢复
	function recovery(){
		$flag=str_replace(" ","",$_GET['flag']);
		$flag=str_replace("-","",$flag);
		$flag=str_replace(":","",$flag);
		$Model = new Model(); // 实例化一个model对象 没有对应任何数据表		
		$sql="SHOW TABLES FROM `".C("DB_NAME")."`";//Tables_in_phptest
		$list=$Model->query($sql);
		if($list)foreach($list as $tmp){
			//删除表
			$delSql="DROP TABLE IF EXISTS `".C("DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."`";
			//echo $delSql."<br>";
			$Model->execute($delSql);
			//查询表结构
			$sql="SHOW CREATE TABLE `".C("BACKUP_DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."_".$flag."`";
			//echo $sql."<br>";
			$create=$Model->query($sql);
			//建备份表
			$createSql=str_replace("`".$tmp["Tables_in_".C("DB_NAME")]."_".$flag."`","`".C("DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."`",$create[0]['Create Table']);
			//echo $createSql."<br>";			
			$Model->execute($createSql);
			//备份数据到备份表
			$insertSql="INSERT INTO `".C("DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."` SELECT * FROM `".C("BACKUP_DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."_".$flag."` ;";
			//echo $insertSql."<br>";			
			$Model->execute($insertSql);
		}
		//exit();
		$this->assign("jumpUrl",__URL__); 				
		$this->success("数据恢复成功!"); 

	}
	//删除备份文件
	function delBackup(){
		$flag=str_replace(" ","",$_GET['flag']);
		$flag=str_replace("-","",$flag);
		$flag=str_replace(":","",$flag);
		$Model = new Model(); // 实例化一个model对象 没有对应任何数据表		
		$sql="SHOW TABLES FROM `".C("DB_NAME")."`";//Tables_in_phptest
		$list=$Model->query($sql);
		if($list)foreach($list as $tmp){
			//删除表
			$delSql="DROP TABLE IF EXISTS `".C("BACKUP_DB_NAME")."`.`".$tmp["Tables_in_".C("DB_NAME")]."_".$flag."`";
			//echo $delSql."<br>";
			$Model->execute($delSql);
		}
		//exit();
		$this->assign("jumpUrl",__URL__); 				
		$this->success("备份数据删除成功!"); 

	}	
}
?>